package com.androidhive.androidlistview;

//import com.androidhive.sessions.SessionManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TextView;

public class FLogin1 extends Activity {
	SessionManager session;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    
	    
	    session = new SessionManager(getApplicationContext());
	    
	    
	    
	    DatabaseHandler db = new DatabaseHandler(this);
	   // setContentView(R.layout.main);
        ScrollView sv = new ScrollView(this);

        TableLayout ll=new TableLayout(this);
        ll.setBackgroundColor(108);
       
        HorizontalScrollView hsv = new HorizontalScrollView(this);
        LayoutParams layoutParams = new LayoutParams(
        	    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

        	layoutParams.setMargins(200, 220, 200, 200);

        	 ll.setLayoutParams(layoutParams); 
        	 ll.setPadding(50, 50, 50, 50);
        	// ll.setBackgroundColor(Color.GREEN);
        	 
        


	    // Get the message from the intent
	    Intent intent = getIntent();
	    String userId = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
	    String Password = intent.getStringExtra(MainActivity.EXTRA_MESSAGE1);
	    if(db.getContactsCount()==0){
	    	
	        db.addContact("Ravi", "9100000000");
	        db.addVendor(1, "Dalmiya Limited", "rice mill", "9035823436", "asdf@gmail.com", "Nagpur");
	        db.addVendor(2, "Agrotech pvt ltd", "oil refinary", "9035823436", "dfghjkl@yahoo.com", "Mumbai");
	        db.addVendor(3, "Sarada and company", "rice mill", "9035823436", "kavita.tawri@gmail.com", "Nagpur");
	        db.addVendor(4, "Mittal pvt. Ltd.", "oil refinary", "9035823436", "mittal@yahoo.com", "Mumbai");
	        Log.d("oncreate ", "success3");
	        
	        db.addVendorlogin(1,"dalmiya", "dalmiya");
	        db.addVendorlogin(2,"agrotech", "agrotech");
	        db.addVendorlogin(3,"sarda", "sarda");
	        db.addVendorlogin(4,"a", "a");
	        Log.d("oncreate ", "success4");
	        
	        db.addToMastercroptable(1, "rice", 1, "Japanese Premium Quality", 8000, 8000,50, "Shalini","Chhattisgarh", 100);
	        db.addToMastercroptable(2, "rice", 1, "Basmati", 6700, 6000,25, "Shalini","Chhattisgarh", 50);
	        db.addToMastercroptable(3, "rice", 4, "Japanese Premium Quality", 9000, 9200,75, "mahesh","Chhattisgarh", 20);
	        db.addToMastercroptable(4, "rice", 6, "Wild Rice", 5600, 5000,80, "suhas","Chhattisgarh", 1000);
	        db.addToMastercroptable(5, "wheat", 7, "VL-832", 6000, 5000,90, "niketan","Maharashtra", 39);
	        db.addToMastercroptable(6, "wheat", 5, "HS-375(Himgiri)", 7800, 7000,100, "rahul","Maharashtra", 70);
	        db.addToMastercroptable(7, "wheat", 8, "Pusa Gold (WR-544)", 2800, 2800,20, "harish","Maharashtra", 89);
	        db.addToMastercroptable(8, "wheat", 2, "HW-2044", 6800, 6700,67, "Deepti","Maharashtra", 200);
	        Log.d("oncreate ", "success5");
	        
	        db.addFarmer(1, "Shalini", "9035823436", "shalini.tawri@gmail.com","Bemetara", "CG");
	        db.addFarmer(2, "Deepti", "9035823436", "deepti@gmail.com", "Durg", "MH");
	        db.addFarmer(3, "ganesh", "9035823436", "ganesh@yahoo.com", "ghakkal", "KA");
	        db.addFarmer(4, "mahesh", "9035823436", "deepti@gmail.com", "Durg", "MH");
	        db.addFarmer(5, "rahul", "9035823436", "deepti@gmail.com", "Durg", "MH");
	        db.addFarmer(6, "suhas", "9035823436", "deepti@gmail.com", "Durg", "MH");
	        db.addFarmer(7, "niketan", "9035823436", "deepti@gmail.com", "Durg", "MH");
	        db.addFarmer(8, "harish", "9035823436", "deepti@gmail.com", "Durg", "MH");
	        Log.d("oncreate ", "success6");

	        
	        db.addFarmerlogin(1, "a", "a");
	        db.addFarmerlogin(2, "Deepti", "Deepti");
	        db.addFarmerlogin(3, "ganesh", "ganesh");
	        db.addFarmerlogin(4, "mahesh", "mahesh");
	        db.addFarmerlogin(5, "rahul", "rahul");
	        db.addFarmerlogin(6, "suhas", "suhas");
	        db.addFarmerlogin(7, "niketan", "niketan");
	        db.addFarmerlogin(8, "harish", "harish");
	        Log.d("oncreate ", "success7");


	        
	      
	      Log.d("sucess","database");

	        }
	    

    	  TextView textView = new TextView(this);
		    textView.setTextSize(20);

    	    int res=db.getFarmerId(userId, Password);
    	 //   db.close();
		try {
    	   
    	   if(res!=0){
    		   session.createLoginSessionVendorId(Integer.toString(res));
    		   session.setIsFarmer("Yes");
    		   Log.d("oncreate ", "Res !0");
    		   Intent intent1 = new Intent(this, FarmerMenu.class);
    		   startActivity(intent1);
    		  
  
    	   }
    	   else{
    		   Log.d("oncreate ", "Res 0");
    	    	textView.setText("Unauthorized access!!!!");
    	    	textView.append("Please Relogin");
    	    	 setContentView(textView);
    	    }
    	} catch (Exception e) {
    		textView.setText(e.toString());
    		 setContentView(textView);
    	}

	    // Create the text view
    	
	  
    	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_display_message, menu);
		return true;
	}

	//@Override
	

}
